package com.banciyuan.bcywebview.biz.main.mainpage.maincircle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.MainCircle;

/**
 * Created by hy on 2015/12/2.
 */
public class MainCircleAdapter extends BaseAdapter {
    private Context mContext;
    private List<MainCircle> list;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public MainCircleAdapter(Context mContext, List<MainCircle> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        MainCircle item = list.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.maincircle_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        imageLoader.displayImage(item.getCover(), viewHolder.head_img, ImageOptions.options);
        viewHolder.head_text.setText(item.getName());
        String updatedTime = item.getUpdated_time();
        long cTime = Long.parseLong(updatedTime);
        long lastTime = SPHelper.getBrowsedCircle(mContext, item.getId());
        if (cTime <= lastTime) {
            viewHolder.update_time.setVisibility(View.GONE);
        } else {
            viewHolder.update_time.setVisibility(View.VISIBLE);
            viewHolder.update_time.setText(StringUtil.getOppositeTime(item.getUpdated_time()) + mContext.getString(R.string.update));
        }
        return convertView;
    }

    public void setdata(List<MainCircle> list) {
        this.list = list;
    }

    static class ViewHolder {
        ImageView head_img;
        TextView head_text;
        TextView update_time;

        public ViewHolder(View convertView) {
            head_img = (ImageView) convertView.findViewById(R.id.iv_work);
            head_text = (TextView) convertView.findViewById(R.id.tv_workname);
            update_time = (TextView) convertView.findViewById(R.id.tv_update_time);
        }
    }

    public void updateView(ListView mListView) {
        if (mListView == null) return;
        int firstIndex = mListView.getFirstVisiblePosition();
        int lastIndex = mListView.getLastVisiblePosition();
        int headerCount = mListView.getHeaderViewsCount();
        for (int i = firstIndex; i < lastIndex; i++) {
            View convertView = mListView.getChildAt(i + headerCount - firstIndex);
            if (convertView == null) continue;
            ViewHolder viewHolder;
            if (convertView.getTag() instanceof ViewHolder)
                viewHolder = (ViewHolder) convertView.getTag();
            else continue;
            if (viewHolder == null) continue;
            if (i < 0 || i >= list.size())
                continue;
            MainCircle item = list.get(i);
            if (null == item)
                continue;
            String updatedTime = item.getUpdated_time();
            long cTime = Long.parseLong(updatedTime);
            long lastTime = SPHelper.getBrowsedCircle(mContext, item.getId());
            if (cTime <= lastTime) {
                viewHolder.update_time.setVisibility(View.GONE);
            } else {
                viewHolder.update_time.setVisibility(View.VISIBLE);
                viewHolder.update_time.setText(StringUtil.getOppositeTime(item.getUpdated_time()) + mContext.getString(R.string.update));
            }
        }

    }
}
